package com.vite.web.controller.monitor;

import com.vite.common.annotation.Log;
import com.vite.common.core.controller.BaseController;
import com.vite.common.core.domain.AjaxResult;
import com.vite.common.core.page.TableDataInfo;
import com.vite.common.enums.BusinessType;
import com.vite.common.utils.poi.ExcelUtil;
import com.vite.framework.web.service.SysPasswordService;
import com.vite.system.domain.SysLoginInfo;
import com.vite.system.service.ISysLoginInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统访问记录
 */
@Tag(name = "系统访问记录")
@RestController
@RequestMapping("/monitor/loginInfo")
@RequiredArgsConstructor
public class SysLoginInfoController extends BaseController {

    private final ISysLoginInfoService infoService;

    private final SysPasswordService passwordService;

    @Operation(summary = "获取系统访问记录列表")
    @PreAuthorize("@ss.hasPermission('monitor:loginInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLoginInfo info) {
        startPage();
        List<SysLoginInfo> list = infoService.selectLoginInfoList(info);
        return getDataTable(list);
    }

    @Operation(summary = "导出系统访问记录列表")
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermission('monitor:loginInfo:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginInfo info) {
        List<SysLoginInfo> list = infoService.selectLoginInfoList(info);
        ExcelUtil<SysLoginInfo> util = new ExcelUtil<SysLoginInfo>(SysLoginInfo.class);
        util.exportExcel(response, list, "登录日志");
    }

    @Operation(summary = "删除系统访问记录")
    @Parameters({
            @Parameter(name = "infoIds", description = "记录id数组", required = true),
    })
    @PreAuthorize("@ss.hasPermission('monitor:loginInfo:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable(name = "infoIds") Long[] infoIds) {
        return toAjax(infoService.deleteLoginInfoByIds(infoIds));
    }

    @Operation(summary = "清除系统访问记录")
    @PreAuthorize("@ss.hasPermission('monitor:loginInfo:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        infoService.cleanLoginInfo();
        return success();
    }

    @Operation(summary = "账户解锁")
    @Parameters({
            @Parameter(name = "userName", description = "用户名", required = true),
    })
    @PreAuthorize("@ss.hasPermission('monitor:loginInfo:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public AjaxResult unlock(@PathVariable("userName") String userName) {
        passwordService.clearLoginRecordCache(userName);
        return success();
    }
}
